//
//  NSArray+YZCategory.m
//  VET
//
//  Created by Lam BG on 2018/7/13.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "NSArray+YZCategory.h"

#import "YZAutoRowModel.h"
#import "YZAutoSectionModel.h"

#import "MJExtension.h"

@implementation NSArray (YZCategory)

- (YZAutoRowModel *)formRowInSection:(NSIndexPath*)indexPath {
    
    YZAutoSectionModel *sectionModel = self[indexPath.section];
    
    return sectionModel.formRows[indexPath.row];;
}

- (YZAutoRowModel *)itemRowInSection:(NSIndexPath*)indexPath {
    
    YZAutoSectionModel *sectionModel = self[indexPath.section];
    
    return sectionModel.itemRows[indexPath.row];;
}

/* 遍历赋值 */
- (void)setAllKeyValues:(NSDictionary*)keyValues {
    
    [self enumerateObjectsUsingBlock:^(NSObject *obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        [obj mj_setKeyValues:keyValues];
    }];
}

/* 获取 api 参数 */
- (NSMutableDictionary*)yz_autoParams {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    for (YZAutoSectionModel *section in self) {
        for (YZAutoRowModel *row in section.formRows) {
            
            if (row.isMust && row.isUnable == NO && (row.paramValue.length == 0)) {
                
                return nil;
            }
            
            // 参数
            if (row.paramValue && row.paramKey && (row.isUnable == NO)) {
                if ([@"objectIds" isEqualToString:row.paramKey]) {
                    
                    // 特殊参数 （免疫）
                    NSString *values = row.paramValue;
                    NSMutableArray *ids = [NSMutableArray array];
                    for (NSString *pigIDs in [values componentsSeparatedByString:@","]) {
                        
                        NSArray *idsArray = [pigIDs componentsSeparatedByString:@"+"];
                        NSString *pigID = idsArray[0];
                        [ids addObject:pigID];
                    }
                    [params setObject:ids forKey:row.paramKey];
                    
                } else if ([@"symptonList" isEqualToString:row.paramKey]) {
                    
                    //TODO:: 支持多选 新增治疗 symptonList 症状（多选）
                    [params setObject:@[@{@"sympton" : row.displayValue}] forKey:row.paramKey];
                    
                } else if ([@"treatmentObjectList" isEqualToString:row.paramKey]) {
                    
                    // 新增治疗 treatmentObjectList 治疗对象
                    NSMutableArray *array = [NSMutableArray array];
                    NSString *values = row.paramValue;
                    if (values.length) {
                        for (NSString *pigIDs in [values componentsSeparatedByString:@","]) {
                            
                            NSArray *idsArray = [pigIDs componentsSeparatedByString:@"+"];
                            NSString *pigID = idsArray[0];
                            [array addObject:@{@"objectId" : pigID, @"objectType" : @"swine"}];
                        }
                    }
                    [params setObject:array forKey:row.paramKey];
                    
                } else if ([@"dieoutObjectSwinery" isEqualToString:row.paramKey]) {
                    
                    // 猪群 死淘登记 （objectType 猪只：swine、猪群：herd）
                    NSMutableArray *array = [NSMutableArray array];
                    NSString *values = row.paramValue;
                    if (values.length) {
                        for (NSString *ids in [values componentsSeparatedByString:@","]) {
                            
                            // 多选 猪群 死淘登记
                            NSArray *idsArray = [ids componentsSeparatedByString:@"+"];
                            NSString *pigID = idsArray[0];
                            NSString *dieoutStatus = @"";
                            if (idsArray.count == 2) dieoutStatus = idsArray[1];
                            [array addObject:@{@"objectId" : pigID, @"dieoutStatus" : dieoutStatus, @"objectType" : @"herd"}];
                        }
                    }
                    [params setObject:array forKey:@"dieoutObjectList"];
                    
                } else if ([@"dieoutObjectSwine" isEqualToString:row.paramKey]) {
                    
                    // 多选 猪只 死淘登记
                    NSMutableArray *array = [NSMutableArray array];
                    NSInteger counter = 0;
                    NSString *values = row.paramValue;
                    if (values.length) {
                        for (NSString *ids in [values componentsSeparatedByString:@","]) {
                            
                            // 多选 猪只 死淘登记
                            NSArray *idsArray = [ids componentsSeparatedByString:@"+"];
                            NSString *pigID = idsArray[0];
                            NSString *dieoutStatus = @"";
                            if (idsArray.count == 2) dieoutStatus = idsArray[1];
                            [array addObject:@{@"objectId" : pigID, @"dieoutStatus" : dieoutStatus, @"objectType" : @"swine"}];
                            counter++;
                        }
                    }
                    [params setObject:[@(counter) stringValue] forKey:@"dieoutNum"];
                    [params setObject:array forKey:@"dieoutObjectList"];
                    
                } else {
                    
                    // 普通参数
                    [params setObject:row.paramValue forKey:row.paramKey];
                }
            }
        }
    }
    
    return params;
}

@end










